/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦，本文采用木兰宽松许可证第2版]
 * 
 * https://zhiqim.org/project/zhiqim_kit/zhiqim_git.htm
 *
 * ZhiqimGit is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.git.command;

import java.io.InputStream;

import org.zhiqim.git.Git;
import org.zhiqim.git.GitCommand;
import org.zhiqim.git.GitConstants;
import org.zhiqim.git.http.model.Advertiser;
import org.zhiqim.git.util.GitHttps;
import org.zhiqim.kernel.constants.HttpConstants;
import org.zhiqim.kernel.httpclient.HttpGetStream;
import org.zhiqim.kernel.util.Asserts;
import org.zhiqim.kernel.util.Strings;
import org.zhiqim.kernel.util.Validates;

/**
 * 查询远程引用列表命令
 *
 * @version v1.4.1 @author zouzhigang 2018-8-3 新建与整理
 */
public class InfoRefsCommand extends GitCommand<Advertiser> implements GitConstants, HttpConstants
{
    private String remoteUrl;
    
    private String service;
    private String userName;
    private String userPass;
    
    public InfoRefsCommand(Git git)
    {
        super(git);
        this.remoteUrl = Strings.addEndsWith(git.getConfig().getString("remote", "origin", "url"), "/");
    }
    
    public InfoRefsCommand setService(String service)
    {
        this.service = service;
        return this;
    }
    
    public InfoRefsCommand setNamePass(String userName, String userPass)
    {
        this.userName = userName;
        this.userPass = userPass;
        return this;
    }
    
    public InfoRefsCommand setRemoteUrl(String remoteUrl)
    {
        this.remoteUrl = Strings.addEndsWith(remoteUrl, "/");
        return this;
    }
    
    /******************************************************************************/
    //检查&调用
    /******************************************************************************/
    
    protected void preprocess()
    {
        Asserts.assertNotEmptyBlank(service, "服务名不能为空白");
        Asserts.as((RECEIVE_PACK.equals(service) || UPLOAD_PACK.equals(service))?null:"服务名只能是[git-receive-pack|git-upload-pack]");
    }

    @Override
    protected Advertiser execute() throws Exception
    {
        HttpGetStream conn = new HttpGetStream(remoteUrl + "info/refs?service="+service);
        conn.addRequestProperty(_USER_AGENT_, ZHIQIM_GIT_VERSION);
        conn.addRequestProperty(_PRAGMA_, _NO_CACHE_);
        conn.addRequestProperty(_CACHE_CONTROL_, _NO_CACHE_MUST_RV_NO_STORE_);
        if (Validates.isNotEmptyBlank(userName) && Validates.isNotEmptyBlank(userPass))
        {//用户名和密码
            conn.addRequestProperty(_AUTHORIZATION_, GitHttps.authorization(userName, userPass));
        }
        conn.execute();
        
        if (!conn.isResponseSuccess())
        {
            throw new Exception("/info/refs时失败：" + conn.getResponseText());
        }
        
        InputStream in = conn.getInputStream();
        Advertiser advertiser = new Advertiser();
        advertiser.parse(in);
        return advertiser;
    }
}
